<template>
  <div class="day-report-entry-container">

    <div class="report-entry-record-box">
      <span class="title">企业名称：</span>
      <span class="entry-name" v-text="company" :class="{'company-text':company != '请选择入职企业'}"
            @click="openCompany"></span>
      <img src="../in.png" class="back" onclick="return false"/>
    </div>

    <div class="report-entry-record-box">
      <span class="title">入职日期:</span>
      <datetime format="YYYY-MM-DD" v-model="workBeginTime" class="birthday" placeholder="请选择入职日期"
                :end-date="endDate"></datetime>
      <img src="../in.png" class="back" onclick="return false"/>
    </div>

    <div class="report-entry-record-box">
      <span class="title">代理人:</span>
      <div class="content-box">
        <input type="text" placeholder="请输入代理姓名" class="content" maxlength="30" v-model="agentName"/>
      </div>
    </div>

    <div class="report-entry-record-box">
      <span class="title">输送来源:</span>
      <popup-picker :data="empOriginList" v-model="empOrigin" class="origin-name" placeholder="请选择输送来源"
                    @on-change="selectOrigin"></popup-picker>
      <img src="../in.png" class="back" onclick="return false"/>
    </div>

    <div class="report-select-write">选填:</div>
    <div class="report-entry-record-box" v-if="batch != 1">
      <span class="title">员工电话:</span>
      <div class="content-box">
        <input type="text" placeholder="请输入员工电话" class="content" maxlength="11" v-model="userTel"/>
      </div>
    </div>
    <div class="report-entry-record-box">
      <span class="title">工价:</span>
      <div class="content-box">
        <input type="text" placeholder="请输入工价" class="content" maxlength="6" v-model="workMoney"/>
        <span class="price-cell">元/时</span>
      </div>
    </div>

    <div class="report-entry-open-box" v-show="showCompany" @click="closeCompany">
      <div class="open-list">
        <div class="name" v-for="(item,index) in companyList" :key="index"
             @click="selectCompany(item.id,item.mechanismName)">{{item.mechanismName}}
        </div>
      </div>
    </div>

    <div class="report-entry-opt-box" v-show="hideBtn">
      <button type="button" class="btn cancel" @click="delReportEmp">删除员工</button>
      <button type="button" class="btn confirm" @click="modifyReportEmp">保存</button>
    </div>
    <backpage @backPage="backPage"></backpage>
  </div>
</template>

<script>
  import {Datetime} from 'vux';
  import {baseUrl} from "../../../config/env";
  import {formatDate} from "../../../config/util";
  import backpage from "../../../components/backpage";
  import {PopupPicker} from 'vux';

  export default {
    data() {
      return {
        empOrigin: [],
        empOriginList: [["大和", "蓝聘", "上势", "大赢和"]],
        originTypeName: this.$route.query.source,
        company: this.$route.query.mechanismName ? this.$route.query.mechanismName : "请选择入职企业",
        userSign: "",
        showCompany: false,
        companyList: [],
        reportId: this.$route.query.reportId,
        mechanismId: this.$route.query.mechanismId,
        workBeginTime: this.$route.query.workBeginTime,
        agentName: this.$route.query.agentName,
        docmHeight: document.documentElement.clientHeight,//默认屏幕高度
        showHeight: document.documentElement.clientHeight,//实时屏幕高度
        hideBtn: true,//显示或者隐藏
        userTel: this.$route.query.userTel,
        workMoney: this.$route.query.workMoney,
        batch: this.$route.query.batch
      }
    },
    created() {
      this.endDate = formatDate(new Date(), "yyyy-MM-dd");
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("success"));
      if (this.originTypeName) {
        this.empOrigin.push(this.originTypeName);
      }
    },
    mounted() {
      this.queryMechanism();
      window.onresize = () => {
        return (() => {
          this.showHeight = document.body.clientHeight;
        })()
      };
    },
    methods: {
      selectOrigin(value) {//员工来源
        this.originTypeName = String(value);
      },
      backPage() {
        this.$router.push("/dayreportlist");
      },
      queryMechanism() {//查询获取企业列表
        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/query_teacher_mechanism?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.get(baseUrl + "/resident/query_teacher_mechanism?sgin=" + this.userSign + "&type=1" + "&auth=" + signature + "&timestamp=" + time).then((res) => {
          let resultObj = res.data;
          if (resultObj.code == 0) {
            if (resultObj.data != null && resultObj.data.length > 0) {
              this.companyList = resultObj.data;
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        })
      },
      openCompany() {//打开企业选择框
        this.showCompany = true;
      },
      closeCompany() {
        this.showCompany = false;
      },
      selectCompany(id, name) {//选择所在企业
        this.showCompany = false;
        this.company = name;
        this.mechanismId = id;
      },
      checkSendData() {
        let value = this.company == "请选择入职企业" ? "" : this.company;
        if (value == "" || value == null || value == undefined) {
          this.$vux.toast.text("请选择入职企业", "middle");
          return;
        }
        if (this.workBeginTime == "" || this.workBeginTime == null || this.workBeginTime == undefined) {
          this.$vux.toast.text("请选择入职日期", "middle");
          return;
        }
        if (this.originTypeName == "" || this.originTypeName == null || this.originTypeName == undefined) {
          this.$vux.toast.text("请选择输送来源", "middle");
          return;
        }
        if (this.agentName == "" || this.agentName == null || this.agentName == undefined) {
          this.$vux.toast.text("请输入代理姓名", "middle");
          return;
        }

        if (this.userTel != '' && this.userTel != null && this.userTel == undefined) {
          let reg1 = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
          if (!reg1.test(this.userTel)) {
            this.$vux.toast.text("请输入正确员工电话", "middle");
            return;
          }
        }
        if (this.workMoney != '' && this.workMoney != null && this.workMoney == undefined) {
          let reg = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
          if (!reg.test(this.workMoney)) {
            this.$vux.toast.text("请输入正确的工价", "middle");
            return;
          }
        }
        return true;
      },
      changeOrigin(value) {//员工来源
        if ("大和" == String(value)) {
          return "DH";
        } else if ("蓝聘" == String(value)) {
          return "LP";
        } else if ("上势" == String(value)) {
          return "SS";
        } else {
          return "DYH";
        }
      },
      modifyReportEmp() {
        if (this.checkSendData()) {
          this.$vux.loading.show({
            text: '加载中',
          });
          let time = (new Date()).getTime();
          let url = baseUrl + "/resident/insert_mechanism_user?timestamp=" + time;
          let param = {"sgin": this.userSign, "timestamp": time};
          let signature = this.$signature(url, param);

          let data = {
            "mechanismId": this.mechanismId,
            "mechanismName": this.company,
            "workBeginTime": this.workBeginTime,
            "agentName": this.agentName,
            "ids": this.reportId.toString().split(","),
            "userTel": this.userTel,
            "workMoney": this.workMoney,
            "source": this.changeOrigin(this.originTypeName)
          };
          this.axios.post(baseUrl + "/resident/insert_mechanism_user?sgin=" + this.userSign + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
            let resultObj = res.data;
            this.$vux.loading.hide();
            if (resultObj.code == 0) {
              if (resultObj.data.result == 0) {
                this.$vux.toast.text(resultObj.data.msg ? resultObj.data.msg : "员工录入失败，请重试", "middle");
              } else {
                this.$router.push("/dayreportlist");
              }
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
            }
          }).catch(() => {
            this.$vux.loading.hide();
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      delReportEmp() {
        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/update_daily_report?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        let data = this.reportId.toString().split(",");
        this.axios.post(baseUrl + "/resident/update_daily_report?sgin=" + this.userSign + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0) {
            if (resultObj.data == 1) {
              this.$router.push("/dayreportlist");
            } else {
              this.$vux.toast.text("删除员工失败，请重试", "middle");
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      }
    },
    watch: {
      showHeight() {
        if (this.docmHeight > this.showHeight) {
          this.hideBtn = false;
        } else {
          this.hideBtn = true;
        }
      }
    },
    components: {
      Datetime,
      backpage,
      PopupPicker
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"

  .day-report-entry-container
    wh(100%, 100%)
    background: #f5f5f5
    .report-entry-record-box
      display: flex
      align-items: center
      wh(100%, 1.28rem)
      background: #FFF
      border-bottom: 0.027rem solid rgba(242, 242, 242, 1)
      .title
        margin-left: 0.32rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .entry-name
        flex: 1
        text-align: right
        height: 0.6rem
        line-height: 0.6rem
        padding-right: 0.253rem
        size-color(0.427rem, #ccc)
        ellipsis()
      .company-text
        color: rgba(51, 51, 51, 1)
      .content-box
        display: flex
        align-items: center
        flex: 1
        height: 1.28rem
        margin-right: 0.32rem
        .content
          width: 100%
          text-align: right
          height: 0.6rem
          size-color(0.427rem, rgba(51, 51, 51, 1))
        .price-cell
          font-size: 0.427rem
          color: rgba(51, 51, 51, 1)
          width: 1.4rem
          text-align: right
      .birthday
        display: flex
        align-items: center
        flex: 1
        text-align: right
        padding-right: 0.267rem
        margin-left: 0.2rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .birthday >>> .weui-cell__ft .vux-cell-placeholder
        color: rgba(204, 204, 204, 1)
      .birthday >>> .weui-cell__ft .vux-cell-value
        color: rgba(51, 51, 51, 1)
      .back
        margin-right: 0.32rem
        wh(0.32rem, 0.32rem)
      .origin-name
        flex: 1
        text-align: right
        height: 0.6rem
        line-height: 0.6rem
        padding-right: 0.253rem
        size-color(0.427rem, #ccc)
      .origin-name >>> .vux-popup-picker-placeholder
        color: rgba(204, 204, 204, 1)
      .origin-name >>> .vux-cell-value
        color: rgba(51, 51, 51, 1)
      .origin-name >>> .weui-cell__ft .vux-cell-placeholder
        color: rgba(204, 204, 204, 1)
      .origin-name >>> .weui-cell__ft .vux-cell-value
        color: rgba(51, 51, 51, 1)
    .report-entry-open-box
      position: fixed
      top: 0
      left: 0
      right: 0
      bottom: 0
      z-index: 88
      fj(center, center)
      background-color: rgba(0, 0, 0, 0.5)
      .open-list
        position: fixed
        z-index: 99
        bottom: 0
        left: 0
        wh(100%, 6.44rem)
        background: #FFF
        overflow: auto
        .name
          fj(center, center)
          height: 1.28rem
          border-bottom: 0.013rem solid #EEE
          cursor: pointer
          size-color(0.44rem, #666666)
          &:hover
            color: #3CAFFF
    .report-entry-opt-box
      position: fixed
      bottom: 0.12rem
      display: flex
      justify-content: center
      align-items: center
      padding-top: 0.96rem
      padding-bottom: 0.64rem
      width: 100%
      height: 1.28rem
      background: rgba(242, 242, 242, 1)
      .btn
        width: 4.267rem
        height: 1.28rem
        line-height: 1.28rem
        font-size: 0.453rem
        border-radius: 0.16rem
        color: rgba(255, 255, 255, 1)
      .cancel
        margin-right: 0.227rem
        background: rgba(255, 83, 83, 1)
      .confirm
        margin-left: 0.227rem
        background: rgba(60, 175, 255, 1)
    .report-select-write
      height: 0.8rem
      line-height: 0.8rem
      font-size: 0.347rem
      color: rgba(153, 153, 153, 1)
      padding-left: 0.32rem
</style>
